package com.fhzn.auth.advice;

import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;

import java.util.stream.Collectors;

/**
 * @author fhzn
 */
public interface ParamValidationErrorResolver {

    ParamValidationErrorResolver HIDE_ERROR_RESOLVER = ex -> "请求参数有误";

    ParamValidationErrorResolver RETURN_ACTUAL_ERROR_RESOLVER = ex -> ex.getAllErrors().stream().map(objectError -> {
        StringBuilder sb = new StringBuilder();
        if (objectError instanceof FieldError) {
            FieldError fieldError = (FieldError) objectError;
            sb.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage());
        } else {
            sb.append(objectError.getObjectName()).append(":").append(objectError.getDefaultMessage());
        }
        return sb.toString();
    }).collect(Collectors.joining(";"));

    String resolve(BindException ex);
}
